package yw.nmh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import yw.nmh.mapper.CityMapper;
import yw.nmh.ranchInfo.domain.City;
import yw.nmh.redis.IRedisService;
import yw.nmh.service.CityService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CityServiceImpl extends ServiceImpl<CityMapper, City>
        implements CityService {

    @Resource
    private IRedisService redisService;;
    @Resource
    private CityMapper cityMapper;

    @Override
    public List<City> getCityList() {
        if (redisService.isExists("city")) {
            return redisService.getValue("city");
        }
        // 1. 获取所有一级地区(省份)
        List<City> provinces = getProvinceList();

        // 2. 获取所有二级地区(城市)并按省份分组
        Map<Integer, List<City>> cityMap = getCityMap();

        // 3. 获取所有三级地区(区县)并按城市分组
        Map<String, List<City>> districtMap = getDistrictMap();

        // 4. 组装三���树形结构
        return buildCityTree(provinces, cityMap, districtMap);
    }

    /**
     * 获取所有省份
     */
    private List<City> getProvinceList() {
        LambdaQueryWrapper<City> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(City::getLevel, 1)
                .orderByAsc(City::getSort);
        return cityMapper.selectList(wrapper);
    }

    /**
     * 获取所有城市并按省份ID分组
     */
    private Map<Integer, List<City>> getCityMap() {
        LambdaQueryWrapper<City> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(City::getLevel, 2)
                .orderByAsc(City::getSort);
        List<City> cities = cityMapper.selectList(wrapper);

        return cities.stream()
                .collect(Collectors.groupingBy(City::getParentId));
    }

    /**
     * 获取所有区县并按城市编码分组
     */
    private Map<String, List<City>> getDistrictMap() {
        LambdaQueryWrapper<City> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(City::getLevel, 3)
                .orderByAsc(City::getSort);
        List<City> districts = cityMapper.selectList(wrapper);

        return districts.stream()
                .collect(Collectors.groupingBy(city -> String.valueOf(city.getParentId())));
    }

    /**
     * 构建省市区三级树形结构
     */
    private List<City> buildCityTree(List<City> provinces,
                                     Map<Integer, List<City>> cityMap,
                                     Map<String, List<City>> districtMap) {
        // 遍历省份
        provinces.forEach(province -> {
            // 获取该省份下的所有城市
            List<City> cities = cityMap.get(province.getId().intValue());
            if (cities != null) {
                // 遍历城市，设置区县
                cities.forEach(city -> {
                    // 获取该城市下的所有区县
                    List<City> districts = districtMap.get(city.getCode());
                    // 设置区县列表
                    city.setChildren(districts);
                });
                // 设置城市列表
                province.setChildren(cities);
            }
        });
        redisService.setValue("city", provinces,30 * 60 * 1000);
        return provinces;
    }
}
